package com.steven.logserver.mq;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

/**
 * mq 配置（使用服务器地址作为队列、交换机、路由键命名关键词）
 *
 * @author keriezhang
 * @date 2016/10/31
 */
@Component
public class DirectExchangeCollocation {

    @Autowired
    private MqCommons mqCommons;

    /**
     * @return DirectExchange
     */
    @Bean
    public DirectExchange defaultExchange(){

        return new DirectExchange(mqCommons.getExchange(),true,false);
    }

    /**
     * 设置队列
     *
     * @return Queue
     */
    @Bean
    public Queue queue() {

        return new Queue(mqCommons.getQueue(), true);
    }

    /**
     * 路由关键字进行绑定,EXCHANGE_A和QUEUE_A按照ROUTINGKEY_A绑定
     *
     * @return Binding
     */
    @Bean
    public Binding bindingExchangeA() {

        return BindingBuilder.bind(queue()).to(defaultExchange()).with(mqCommons.getRoutingKey());
    }
}
